package com.cmwa.solution.sys.permission.dataRole.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.type.Alias;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.solution.base.controller.BaseController;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.exception.ValidateFailedException;
import com.cmwa.solution.sys.dictionary.entity.DictionaryVo;
import com.cmwa.solution.sys.dictionary.service.DictionaryService;
import com.cmwa.solution.sys.msg.vo.OperationResultVo;
import com.cmwa.solution.sys.permission.dataAuthority.entity.RoleVo;
import com.cmwa.solution.sys.permission.dataRole.service.RoleService;
import com.cmwa.solution.utils.Constant;


/**
 * 角色管理控制器
 * @author ex-weicb
 * @Date 2016年4月26日 下午6:32:35
 * @Description
 */
@Controller
@RequestMapping(value = "/sys/role")
@Alias("roleController1")
public class RoleController extends BaseController {
	/**
	 * 设置jsp的访问路径
	 */
	private final static String PAGE_PATH = "system/sys/permission/dataRole/";
	@Resource
	private RoleService roleService;
	@Resource
	private DictionaryService dictionaryService;
	
	/**
	 * 角色维护列表页面
	 * 
	 * @return
	 */
	@RequestMapping("/roleListView")
	public String roleListView(ModelMap model) {
		DictionaryVo dictionaryVo=new DictionaryVo();
		dictionaryVo.setDctFathType(Constant.DCTTYPE_ROLE_XXX_TYPE);
		dictionaryVo.setDctRootType(Constant.DCTTYPE_ROLE_XXX_TYPE);
		List<DictionaryVo> dictionaryList=dictionaryService.listDictionary(dictionaryVo);
		model.addAttribute("dictionaryList", dictionaryList);
		return PAGE_PATH + "roleList";
	}
	
	/**
	 * 角色维护列表分页数据
	 * 
	 * @param sp
	 * @return
	 */
	@RequestMapping("/roleListPage")
	@ResponseBody
	public Page<RoleVo> roleListPage(SearchParam sp) {
		Page<RoleVo> list = roleService.roleListPage(sp);
		return list;
	}
	
	/**
	 * 角色维护详情页面
	 * 
	 * @return
	 */
	@RequestMapping("/roleDetailView")
	public String roleDetailView(@RequestParam("roleId")String roleId,ModelMap model) {
		RoleVo role=roleService.queryByRoleId(roleId);
		String empName = role.getEmpsName();
		if(!StringUtils.isEmpty(empName)){
			String [] empNames = empName.split(",");
			model.addAttribute("empNames", empNames);
		}
		model.addAttribute("role", role);
		model.addAttribute("roleShareInfo", roleService.queryShareInfoById(roleId));
		return PAGE_PATH + "roleDetail";
	}
	
	/**
	 * 角色维护新增页面
	 * 
	 * @return
	 */
	@RequestMapping("/roleAddView")
	public String roleAddView(ModelMap model) {
		DictionaryVo dictionaryVo=new DictionaryVo();
		dictionaryVo.setDctFathType(Constant.DCTTYPE_ROLE_XXX_TYPE);
		dictionaryVo.setDctRootType(Constant.DCTTYPE_ROLE_XXX_TYPE);
		List<DictionaryVo> dictionaryList=dictionaryService.listDictionary(dictionaryVo);
		model.addAttribute("roleTypeList", dictionaryList);//角色类型数据字典集合
		return PAGE_PATH + "roleAdd";
	}

	/**
	 * 保存新增角色
	 * 
	 * @param sp
	 * @return
	 * @throws ValidateFailedException 
	 */
	@RequestMapping("/saveRole")
	public String saveRole(RoleVo role,ModelMap model) throws ValidateFailedException {
		String message=roleService.saveRole(role);
		if(!StringUtils.isEmpty(message)){
			OperationResultVo qr=new OperationResultVo();
			qr.setResultContent(message);
			model.addAttribute("custom", qr);
			return redirectCustom();
		}
		return redirectSuccess();
	}
	
	/**
	 * 查找角色数量
	 * 
	 * @param sp
	 * @return
	 */
	@RequestMapping("/queryRoleCount")
	@ResponseBody
	public Object queryRoleCount(RoleVo role) {
		int count = roleService.queryRoleCount(role);
		return count;
	}
	
	/**
	 * 查找角色
	 * 
	 * @param sp
	 * @return
	 */
	@RequestMapping("/queryRole")
	@ResponseBody
	public Object queryRole(RoleVo role) {
		List<RoleVo> roles= roleService.queryRole(role);
		return roles;
	}
	
	/**
	 * 角色维护修改页面
	 * 
	 * @return
	 */
	@RequestMapping("/roleUpdateView")
	public String roleUpdateView(@RequestParam("roleId")String roleId,ModelMap model) {
		DictionaryVo dictionaryVo=new DictionaryVo();
		dictionaryVo.setDctFathType(Constant.DCTTYPE_ROLE_XXX_TYPE);
		dictionaryVo.setDctRootType(Constant.DCTTYPE_ROLE_XXX_TYPE);
		List<DictionaryVo> dictionaryList=dictionaryService.listDictionary(dictionaryVo);
		model.addAttribute("roleTypeList", dictionaryList);//角色类型数据字典集合
		RoleVo role=roleService.queryByRoleId(roleId);
		model.addAttribute("role", role);
		model.addAttribute("roleShareInfo", roleService.queryShareInfoById(roleId));
		return PAGE_PATH + "roleUpdate";
	}
	
	/**
	 * 保存修改角色
	 * 
	 * @param sp
	 * @return
	 * @throws ValidateFailedException 
	 */
	@RequestMapping("/updateRole")
	public String updateRole(RoleVo role,ModelMap model) throws ValidateFailedException {
		String message=roleService.updateRole(role);
		if(!StringUtils.isEmpty(message)){
			OperationResultVo qr=new OperationResultVo();
			qr.setResultContent(message);
			model.addAttribute("custom", qr);
			return redirectCustom();
		}
		return redirectSuccess();
	}
}
